<template>
	<view class="content">
		<uni-easyinput v-model="name" placeholder="歌名"></uni-easyinput>
		<button @click="find()">搜索</button>
		<view style="text-align: center;">点击歌曲上传</view>
		<uni-list>
			<uni-list :border="true" v-for="(item,index) in musicList">
				<uni-list-chat 
					clickable
					:title="item.name" 
					:avatar="item.img" 
					:note="item.arname+'  '+item.alname"
					@click="clickMusic(item)"
				>
				</uni-list-chat>
			</uni-list>
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'FindCloudMusicList',
				name: '',
				musicList: []
			}
		},
		onLoad() {

		},
		methods: {
			async find() {
				uni.showLoading({
					title: '搜索中',
					mask:true
				});
				let res = await this.NeteaseCloudMusicTool.findMusic(this, this.name)
				uni.hideLoading();
				if (res.code === 200) {
					this.musicList = []
					for (var i = 0; i < res.result.songs.length; i++) {
						let remusic = res.result.songs[i]
						let arname = ''
						for (var j = 0; j < remusic.ar.length; j++) {
							arname = arname+remusic.ar[j].name
							if(j !== (remusic.ar.length-1)){
								arname = arname+","
							}
						}
						let music = {
							"name": remusic.name,
							"arname": arname,
							"alname": remusic.al.name,
							"img": remusic.al.picUrl,
							"userid": this.Device.deviceId,
							"url": null,
							"thirdid": remusic.id+""
						}
						this.musicList.push(music)
					}
				}
			},
			async clickMusic(music){
				console.log(music)
				uni.showLoading({
					title: '获取URL',
					mask:true
				});
				let res = await this.NeteaseCloudMusicTool.findMusicUrl(this, music.thirdid)
				uni.hideLoading();
				if (res.code !== 200 ) {
					return
				}
				if(res.data === undefined || res.data === null|| res.data.length === 0){
					return
				}
				if(res.data[0].url === undefined || res.data[0].url === null|| res.data[0].url === ''){
					return
				}
				music.url = res.data[0].url
				console.log(music);
				uni.showLoading({
					title: '上传中',
					mask:true
				});
				res = await this.HttpTool.UpMusicByUrl(this, music)
				uni.hideLoading();
			}
		}
	}
</script>
<style>
</style>
